var goodsModal = function(p){
	var that = {};
	
	var wrapper,container,item_view,trigger,ptree,title;
	wrapper = (p.container) ? p.container+"w" : "#modal_container";
	container = (p.container) ? p.container : "#selected_category";
	item_view = (p.container) ? p.container + "_item_view" : "#item_view";
	title = (p.title) ? p.title : "選択してください";
	
	//モーダルウインドウを格納するノードを追加して、対象モジュールをload
	
	var html = '<div id="' + wrapper.replace('#','') + '" class="mcon">';
	html += '<span class="close_btn"><a href="javascript:;">b</a></span>';
	html += '<h3>' + title + '</h3>';
	html += '<div id="' + item_view.replace('#','') + '"></div></div>';
	
	$('body').append(html);
	$(item_view).load(p.url,
	function(){
		
		$(wrapper).resizable({
			alsoResize: '.modal-tree,.tree_right',
			minWidth:700
		});
		
		if(p.mode == "single"){
			$(item_view + ' .selected_container').remove();
		}
		
		trigger = (p.trigger) ? p.trigger : ".btn_category";
		ptree =(p.container) ? p.container+'_tree' : "#category";
		$(".tree_left > ul",item_view).attr('id',ptree.replace('#',''));
		setTreeView(ptree);		
		$(wrapper).draggable({handle:"h3"});
		that.setModal();
	});
	
	that.setModal = function(){
		
		//アイテム削除イベントの設定
		that.setDelClick();

		$(trigger).click(function(){
			that.toggleModal(true);
		});
		
		$(item_view + " .cid").click(function(){
			
			//ここでリストをajaxで取得
			
			var html = '<li name="' + $(this).parent().attr('id') + '"><a href="javascript:;" class="delete_category">削除</a>' + $(this).text() + '</li>';
			
			$(item_view + ' .tree_right ul.selected_goods')
				.html('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>')
				.append('<li name="' + $(this).parent().attr('id') + '"><img src=".gif" /><br />' + $(this).text() + '</li>');
				
				if(p.mode == "single"){
					$(item_view + ' .tree_right ul.selected_goods li').click(function(){
						that.toggleModal(false);
						var ids = $(this).attr('name') + ",";
						that.setCategory( ids );
					});
				} else {
					
					var setInDel = function(){
						$(item_view + ' .selected_container li').unbind().click(function(){
							var n = $(this).attr('name');
							$(item_view + '.selected_goods li[name=' + n + ']').removeClass("selected");
							$(this).remove();
						})				
					}
					
					$(item_view + ' .tree_right ul.selected_goods li').click(function(){
						if(!$(this).hasClass('selected')){
							$(this).addClass('selected')
							$(item_view + ' .selected_container ul')
								.append('<li name="' + $(this).attr('name') + '"><img src=".gif" /><br />' + $(this).text() + '</li>');
							setInDel();
						}
					});
					
					$(item_view + ' .tree_right p a').click(function(){
						that.toggleModal(false);
						var ids = "";
						$(item_view + ' .selected_container li').each(function(){
							ids += $(this).attr("name") + ",";
						});
						that.setCategory( ids );
					});
				}				
		});
	};
	
	that.toggleModal = function(s){
		if(s == true){
			//scrollToTop();
			
			if($(item_view + ' .tree_right').size() > 0){
				$(item_view + ' .tree_right ul').html("");
				$(item_view + ' .cid').removeClass("selected");
			}
			
			$('body').append('<div id="modal_background">&nbsp;</div>');
			$('#modal_background').fadeIn(300).height($(document).height());
			$(wrapper).show().css({"position":"absolute","opacity":0,'top':($(document).scrollTop())+'px'}).animate({"opacity":1,"top":($(document).scrollTop()+50)+"px"},300);
			$('#modal_background , .close_btn').click(function(){
				that.toggleModal(false);
			});
		}else {
			$('#modal_background').fadeOut(300,function(){$(this).remove();});
			$(wrapper)
				.animate({"position":"absolute","opacity":0,"top":$(document).scrollTop()+'px'},300,
				function(){
					$(this).hide();
				});
		}
	};
	
	that.setCategory = function(ids){
		
		if(ids != ""){
		
			ids = ids.split(",");
			for(var i=0; i < ids.length-1; i++ ){
				url  = './get_category.php?id=' + ids[i];
				$.ajax({
					type: 'GET',
					url: url,
					datatype: 'json',
					success: function(jsonData){
						var jsonObj = eval(jsonData);
						exist = false;
						$(container + ' li').each( function(){
							if(typeof jsonObj[0] != "undefined" && $(this).attr('id') == jsonObj[0].id ){
								exist = true;
							}
						});
						if( !exist ){
							str = '<li id="' + jsonObj[0].id + '"><a href="javascript:;" class="delete_category">削除</a> ' + jsonObj[0].name + '</li>';
							$(container).append(str).fadeIn();
							that.setDelClick();
							
							if(p.mode == "single"){
								$(trigger).hide();
							}
							
						}
					},
					error: function(xhr, status, err) {
						alert(err);
					}
				});
			}
			
			if(p.mode != "single"){
				that.sortable_category();
			}
		}
	};
	
	that.setDelClick = function(){
		$('.delete_category').unbind().click(function(){
			$(this).parent().remove();
			if($(container + ' li').size() == 0){
				$(".th_span").remove();
				if(p.mode == "single"){
					$(trigger).show();
				}
			}
		});
	};
	
	that.sortable_category = function(){
		
		if($('.th_span',$(container).closest('tr')).size() == 0){
			$('th',$(container).closest('tr')).append("<span class='th_span'><br />ドラッグ&ドロップで<br />並び替え出来ます。</span>");
			$(".th_span").fadeIn(1000);
		}
		
		$(container).sortable({
			start: function(event, ui) {
				$("body").append("<div id='righttopmsg'>並べたい位置に移動して、クリックを離して下さい。</div>");
				$('#righttopmsg').show(300);
			},
			stop: function(event, ui) {
				$('#righttopmsg').fadeOut(300,function(){$(this).remove();});
			}
		});
		$(container).disableSelection();
	};
		
	that.set_item = function( id ){
		idx = 'item_id' + $('#rank_no').val();
		$('#' + idx).val( id );
		that.get_item( id );
	}
	
	return that;
};